/// @tags:
#include <cstdio>
#include <cstring>
#include <iostream>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

constexpr int maxn = 5e2 + 1;

std::string map[maxn];

int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
      cin >> map[i];
    }
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        if (map[i][j] == '1') ++ans;
      }
    }
    int cnt = 2;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        if (map[i][j] == '0') cnt = std::min(cnt, 1);
        if (j < m - 1 && map[i][j] == '0' && map[i][j + 1] == '0') {
          cnt = 0;
        }
        if (i < n - 1 && map[i][j] == '0' && map[i + 1][j] == '0') {
          cnt = 0;
        }
        if (i < n - 1 && j < m - 1 && map[i][j] == '0' && map[i + 1][j + 1] == '0') cnt = 0;
        if (i < n - 1 && j > 0 && map[i][j] == '0' && map[i + 1][j - 1] == '0') cnt = 0;
      }
    }
    cout << ans - cnt << '\n';
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("C_Corners.in", "r", stdin);
  freopen("C_Corners.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
